package com.cze.servlet;

import com.cze.service.PersonService;
import com.cze.vo.Person;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/User")
public class UserServlet extends HttpServlet {

    private PersonService personService = (PersonService) LoderServlet.getBean("personServiceImpl");

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String path = "/user/login.jsp";
        String op = req.getParameter("op");
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        String name;
        Person person;
        switch (op) {
            case "login":
                person = new Person(id, "", password);
                if (personService.signIn(person)) {
                    req.getSession().setAttribute("id", id);
                    resp.sendRedirect(req.getContextPath()+"/Product?op=manage&page=0&limit=10");
                    return;
                } else {
                    req.setAttribute("info", "密码错误或账号不存在");
                }
                break;
            case "modify":
                if (req.getSession().getAttribute("id") != null) {
                    name = req.getParameter("name");
                    person = new Person(id, name, password);
                    req.setAttribute("flag", personService.modify(person));
                    resp.sendRedirect(req.getContextPath()+"/User?op=modify&id=" + id);
                    return;
                } else {
                    req.setAttribute("info", "请先登录");
                }
                break;
            case "register":
                name = req.getParameter("name");
                person = new Person(id, name, password);
                System.out.println(person);
                if (personService.signUp(person)) {
                    req.setAttribute("info", "注册成功，请登录");
                } else {
                    req.setAttribute("info", "注册失败，可能该账号已注册");
                }
                path = "/user/login.jsp";
                break;
        }
        req.getRequestDispatcher(path).forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getSession().getAttribute("id") == null) {
            resp.sendRedirect(req.getContextPath()+"/user/login.jsp");
            return;
        }
        String path = "/user/login.jsp";
        String op = req.getParameter("op");
        String personID;
        switch (op) {
            case "destroy":
                personID = req.getParameter("id");
                personService.destroy(personID);
                req.setAttribute("info", "成功销毁该账号");
            case "logout":
                req.getSession().invalidate();
                break;
            case "modify":
                personID = req.getParameter("id");
                req.setAttribute("person", personService.findByID(personID));
                path = "/user/modify.jsp";
                break;
        }
        req.getRequestDispatcher(path).forward(req, resp);
    }
}
